#include "parafluiddialogsimulation.h"
#include "parafluidvector.h"

#include <QtDebug>

using namespace ParaFluid;

DialogSimulation::~DialogSimulation()
{
	/* does nothing */
}

DialogSimulation::DialogSimulation(QWidget * parent, Simulation * simulation)
	: QDialog(parent)
	, _simulation(simulation)
{
	setupUi(this);
	
	sync();
}

void DialogSimulation::sync()
{
	disconnect(toolButtonSimulate            , SIGNAL(clicked()), this, SLOT(pause()));
	disconnect(pushButtonResetSimulation     , SIGNAL(clicked()), this, SLOT(resetSimulation()));
	disconnect(pushButtonResetParameters     , SIGNAL(clicked()), this, SLOT(resetParameters()));
	disconnect(doubleSpinBoxSmoothingDistance, SIGNAL(valueChanged(double)), this, SLOT(setSmoothingDistance(double)));
	disconnect(doubleSpinBoxTimeStep         , SIGNAL(valueChanged(double)), this, SLOT(setTimeStep(double)));
	disconnect(doubleSpinBoxMass             , SIGNAL(valueChanged(double)), this, SLOT(setSubstanceMass(double)));
	disconnect(doubleSpinBoxPressureFactor   , SIGNAL(valueChanged(double)), this, SLOT(setSubstancePressureFactor(double)));
	disconnect(doubleSpinBoxRestDensity      , SIGNAL(valueChanged(double)), this, SLOT(setSubstanceRestDensity(double)));
	disconnect(doubleSpinBoxViscosityFactor  , SIGNAL(valueChanged(double)), this, SLOT(setSubstanceViscosityFactor(double)));
	disconnect(doubleSpinBoxGravityX         , SIGNAL(valueChanged(double)), this, SLOT(setGravity()));
	disconnect(doubleSpinBoxGravityY         , SIGNAL(valueChanged(double)), this, SLOT(setGravity()));
	disconnect(doubleSpinBoxGravityZ         , SIGNAL(valueChanged(double)), this, SLOT(setGravity()));
	
	toolButtonSimulate->setDown(_simulation->isRunning());
	doubleSpinBoxSmoothingDistance->setValue(_simulation->sph().smoothingDistance());
	doubleSpinBoxTimeStep->setValue(_simulation->sph().timeStep());
	doubleSpinBoxMass->setValue(_simulation->substances().at(0).mass);
	doubleSpinBoxPressureFactor->setValue(_simulation->substances().at(0).pressureFactor);
	doubleSpinBoxRestDensity->setValue(_simulation->substances().at(0).restDensity);
	doubleSpinBoxViscosityFactor->setValue(_simulation->substances().at(0).viscosityFactor);
	doubleSpinBoxGravityX->setValue(_simulation->sph().gravity().x);
	doubleSpinBoxGravityY->setValue(_simulation->sph().gravity().y);
	doubleSpinBoxGravityZ->setValue(_simulation->sph().gravity().z);
	
	connect(toolButtonSimulate            , SIGNAL(clicked()), this, SLOT(pause()));
	connect(pushButtonResetSimulation     , SIGNAL(clicked()), this, SLOT(resetSimulation()));
	connect(pushButtonResetParameters     , SIGNAL(clicked()), this, SLOT(resetParameters()));
	connect(doubleSpinBoxSmoothingDistance, SIGNAL(valueChanged(double)), this, SLOT(setSmoothingDistance(double)));
	connect(doubleSpinBoxTimeStep         , SIGNAL(valueChanged(double)), this, SLOT(setTimeStep(double)));
	connect(doubleSpinBoxMass             , SIGNAL(valueChanged(double)), this, SLOT(setSubstanceMass(double)));
	connect(doubleSpinBoxPressureFactor   , SIGNAL(valueChanged(double)), this, SLOT(setSubstancePressureFactor(double)));
	connect(doubleSpinBoxRestDensity      , SIGNAL(valueChanged(double)), this, SLOT(setSubstanceRestDensity(double)));
	connect(doubleSpinBoxViscosityFactor  , SIGNAL(valueChanged(double)), this, SLOT(setSubstanceViscosityFactor(double)));
	connect(doubleSpinBoxGravityX         , SIGNAL(valueChanged(double)), this, SLOT(setGravity()));
	connect(doubleSpinBoxGravityY         , SIGNAL(valueChanged(double)), this, SLOT(setGravity()));
	connect(doubleSpinBoxGravityZ         , SIGNAL(valueChanged(double)), this, SLOT(setGravity()));
}

void DialogSimulation::pause()
{
	_simulation->pause();
}

void DialogSimulation::resetSimulation()
{
	_simulation->resetSimulation();
	_simulation->insertAtoms(0, spinBoxAtoms->value());
}

void DialogSimulation::resetParameters()
{
	_simulation->resetParameters();
	sync();
}

void DialogSimulation::setSmoothingDistance(double value)
{
	_simulation->setSmoothingDistance(value);
}

void DialogSimulation::setTimeStep(double value)
{
	_simulation->setTimeStep(value);
}

void DialogSimulation::setGravity()
{
	_simulation->setGravity(Vector(doubleSpinBoxGravityX->value(), doubleSpinBoxGravityY->value(), doubleSpinBoxGravityZ->value()));
}

void DialogSimulation::setSubstanceMass(double value)
{
	_simulation->setSubstanceMass(0, value);
}

void DialogSimulation::setSubstancePressureFactor(double value)
{
	_simulation->setSubstancePressureFactor(0, value);
}

void DialogSimulation::setSubstanceRestDensity(double value)
{
	_simulation->setSubstanceRestDensity(0, value);
}

void DialogSimulation::setSubstanceViscosityFactor(double value)
{
	_simulation->setSubstanceViscosityFactor(0, value);
}

